package com.example.lianxi222.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.lianxi222.MainActivity;
import com.example.lianxi222.R;
import com.example.lianxi222.databinding.ActivityLoginBinding;
import com.example.lianxi222.register.RegisterActivity;
import com.example.mvvm_base.mvvm.db.AppDateBaseUtils;
import com.example.mvvm_base.net.Const;
import com.example.mvvm_base.userbean.Money;
import com.example.mvvm_base.userbean.UserEntity;
import com.example.mvvm_base.view.BaseView;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseView<ActivityLoginBinding,LoginViewModel> {




    @Override
    protected void initData() {
    viewModel.loginSuccess.observe(this, userEntity -> {
        if(userEntity.getCode()==200){
            ToastUtils.showLong("("+userEntity.getMessage()+")"+"欢迎回来:"+userEntity.getData().getUsername());
            //环信登录
            EMClient.getInstance().login(userEntity.getData().getUsername(), userEntity.getData().getPassword(), new EMCallBack() {
                @Override
                public void onSuccess() {
                    ToastUtils.showLong("环信登录成功");
                }

                @Override
                public void onError(int code, String error) {
                  ToastUtils.showLong(error);
                }
            });

            //sp储存token
            SPUtils.getInstance().put(Const.SP_TOKEN,userEntity.getData().getToken());
            //获取登录信息到数据库
            AppDateBaseUtils.getInstance().getUserDao().insertUser(userEntity.getData());
            //sp储存用户名
            SPUtils.getInstance().put(Const.SP_USERNAME,userEntity.getData().getUsername());
            //给用户充值money
            Money money = new Money(0,userEntity.getData().getUsername(),10000L);
            //添加到数据库
            AppDateBaseUtils.getInstance().getMoneyDao().addMoney(money);
            //跳转
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }else{
            ToastUtils.showLong(userEntity.getMessage());
        }
    });
    viewModel.loginFailure.observe(this, s -> ToastUtils.showLong("登录失败"+s));

    }

    @Override
    protected void initControlView() {
        viewDataBind.login.setOnClickListener(view -> viewModel.login(viewDataBind.loginUsername.getText().toString(),viewDataBind.loginPassword.getText().toString()));
       viewDataBind.toRegister.setOnClickListener(view -> {
           Intent intent = new Intent(this, RegisterActivity.class);
           startActivity(intent);
       });
       //退出登录按钮
       viewDataBind.logout.setOnClickListener(view -> {
        EMClient.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                ToastUtils.showLong("退出成功");
            }

            @Override
            public void onError(int code, String error) {

            }
        });
       });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }
}